import { createIcon } from '@/utils/IconUtil';
import { request } from '@umijs/max';

/**
 * 调用接口获取菜单
 */
export async function getRouters(): Promise<any> {
  return request('/api/system/menu/getRouters');
}

/**
 * 获取菜单路由
 */
export async function getMenu() {
  const res = await getRouters();
  if (res.code === 200) {
    return convertCompatRouters(res.data);
  } else {
    return [];
  }
}

/**
 * 将菜单路由转换成antdpro的格式
 * @param childrens
 */
export function convertCompatRouters(childrens: API.RoutersMenuItem[]): any[] {
  return childrens.map((item: API.RoutersMenuItem) => {
    return {
      path: item.path,
      icon: createIcon(item.meta.icon),
      //  icon: item.meta.icon,
      name: item.meta.title,
      routes: item.children ? convertCompatRouters(item.children) : undefined,
      hideChildrenInMenu: item.hidden,
      hideInMenu: item.hidden,
      component: item.component,
      authority: item.perms,
    };
  });
}



